package com.example.superoa.model.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.dao.ApprovalDao;
import com.example.superoa.model.dao.RapplyDao;
import com.example.superoa.model.dao.SalarySubDao;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.dao.TeacherDao;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.mappers.AttendMapper;
import com.example.superoa.model.mappers.BusinessMapper;
import com.example.superoa.model.mappers.LeaveMapper;
import com.example.superoa.model.mappers.OvertimeMapper;
import com.example.superoa.model.pojos.Approval;
import com.example.superoa.model.pojos.Rapply;
import com.example.superoa.model.pojos.SalarySub;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.pojos.Teacher;

@Transactional(rollbackFor=Exception.class)
@Service
public class MenuService {
	//加班
	@Autowired 
	private OvertimeMapper j_map;
	
	@Autowired
	private LeaveMapper q_map;
	
	@Autowired
	private AttendMapper b_map;
	
	@Autowired
	private BusinessMapper c_map;
	
	@Autowired
	private AttendMonthService am_ser;
	
	@Autowired
	private AttendService  a_ser;
	
	@Autowired
	private ApprovalDao ad;
	
	@Autowired
	private StaffDao sd;
	
	@Autowired
	private TeacherDao td;
	
	@Autowired
	private SalarySubDao ssd;
	
	@Autowired
	private RapplyDao ry;
	
	private Shijian sj=new Shijian();
	
	public JSONObject selectStaff(Integer id) {
		Integer staff_id=Integer.parseInt(a_ser.selectDeptStaff(id));
		JSONObject staff=a_ser.selectstaff(staff_id);
		return staff;
	}
	public JSONObject selectShenpi(Integer type,Integer id) {
		JSONArray s=new JSONArray();
		if(type==1) {
			selectchuchai(c_map.selectBusinessid(id),s);
		}else if(type==2) {
			selectbuka(b_map.selectshenqiid(id),s);
		}else if(type==3) {			
			selectjiaban(j_map.selectOvertimeid(id),s);
		}else if(type==4) {
			selectqingjia(q_map.selectLeaveid(id),s);
		}else if(type==5) {
			selectteach(j_map.applyxiangqing(id),s);
		}else if(type==6) {
			findsubsh(j_map.subxiangqing(id),s);
		}else if(type==7) {
			selectteach1(j_map.applyxiangqing1(id),s);
		}else if(type==8) {
			rapplyfsp(j_map.rapplyxq(id),s);
		}
		return s.getJSONObject(0); 
	}

	//通过搜索条件处理JSONArray
	public JSONArray selectsousuo(JSONArray arry,Integer type,Date date1,Date date2) {
		JSONArray json=new JSONArray();
		for (Object object : arry) {
			JSONObject o=(JSONObject)object;
			Date nowdate=o.getDate("sdate");
			if(o.getInteger("lei")==type||type==0) {
				if(date1==null&&date2!=null) {
					if(sj.getPanduanShi2(nowdate, date2)<=0) {
						json.add(object);
					}
				}else if(date2==null&&date1!=null) {
					if(sj.getPanduanShi2(nowdate, date1)>=0) {
						json.add(object);
					}	
				}else if(date2!=null&&date1!=null){
					if(sj.belongCalendar(nowdate, date1, date2, 2)) {
						json.add(object);
					}
				}else {
					json.add(object);
				}
			}
		}
		return json;
	}
	//把数组根据时间排序，并转化
	public JSONArray selectArrayDate(JSONArray json) {
		JSONArray jsonarry=sj.paixuJSONArray(json);
		SimpleDateFormat sdf;
		String str="yyyy-MM-dd HH:mm:ss";
		for (Object object : jsonarry) {
			JSONObject obj=(JSONObject)object;
			if(obj.getInteger("lei")==2) {
				sdf=new SimpleDateFormat("HH:mm:ss");
			}else {
				sdf=new SimpleDateFormat("yyyy-MM-dd");
			}
			if(obj.get("kai")!=null) {
				obj.put("kai", sdf.format(new Date(obj.getLong("kai"))));
			}
			if(obj.get("jie")!=null) {
				obj.put("jie", sdf.format(new Date(obj.getLong("jie"))));
			}
			sdf=new SimpleDateFormat(str);
			if(obj.get("sdate")!=null) {
				obj.put("sdate", sdf.format(new Date(obj.getLong("sdate"))));
			}
			if(obj.get("pdate")!=null) {
				obj.put("pdate", sdf.format(new Date(obj.getLong("pdate"))));
			}
		}
		return jsonarry;
	}
	public JSONArray selectOvertime(Integer Staff_id) {
		JSONArray jiaban=j_map.selectOvertimeID(Staff_id);
		JSONArray qingjia=q_map.selectLeaveID(Staff_id);
		JSONArray buka=b_map.selectattendID(Staff_id);
		JSONArray chuchai=c_map.selectBusinessID(Staff_id);
		JSONArray teach=j_map.selectteach(Staff_id);
		JSONArray teach1=j_map.selectteach1(Staff_id);
		JSONArray sp=j_map.rapplyfsp(Staff_id);
		JSONArray sqr=j_map.findsubsh(Staff_id);
		JSONArray shengqing=new JSONArray();
		selectqingjia(qingjia,shengqing);
		selectjiaban(jiaban, shengqing);
		selectbuka(buka,shengqing);
		selectchuchai(chuchai,shengqing);
		selectteach(teach,shengqing);
		selectteach1(teach1,shengqing);
		rapplyfsp(sp,shengqing);
		findsubsh(sqr,shengqing);	
		return selectArrayDate(shengqing);
	}
	public JSONArray selectStaffShen(Integer Staff_id) {
		JSONArray jiaban=j_map.selectOvertimeIDD(Staff_id);
		JSONArray qingjia=q_map.selectLeaveIDD(Staff_id);
		JSONArray buka=b_map.selectattendIDD(Staff_id);
		JSONArray chuchai=c_map.selectBusinessIDD(Staff_id);
		JSONArray teach=j_map.selectstaffapply(Staff_id);
		JSONArray teach1=j_map.selectstaffapply1(Staff_id);
		JSONArray sq=j_map.rapplysq(Staff_id);
		JSONArray sqr=j_map.findsqr(Staff_id);
		JSONArray shengqing=new JSONArray();
		selectqingjia(qingjia,shengqing);
		selectjiaban(jiaban, shengqing);
		selectbuka(buka,shengqing);
		selectchuchai(chuchai,shengqing);
		selectteach(teach,shengqing);
		selectteach1(teach1,shengqing);
		rapplyfsp(sq,shengqing);
		findsubsh(sqr,shengqing);
		return selectArrayDate(shengqing);
	}
	public JSONArray selectchuchai(JSONArray chuchai,JSONArray arr) {
		for (Object object : chuchai) {
			JSONObject obj=new JSONObject();
			JSONObject o=(JSONObject)object;
			obj.put("id", o.get("bus_id"));
			obj.put("lei", 1);
			if(o.getInteger("bus_chu")==1) {
				obj.put("type","出差申请");
			}else {
				obj.put("type","出差补录");
			}
			obj.put("dizhi",  o.get("bus_sheng"));
			obj.put("staff_id", o.getInteger("staff_id"));
			obj.put("pdate", o.get("bus_sdate"));
			obj.put("yuan",o.get("bus_why"));
			obj.put("noyuan",o.get("bus_nowhy"));
			obj.put("sdate", o.get("bus_date"));
			obj.put("name", o.get("Staff_name"));
			obj.put("zhiwei", am_ser.select(o.getInteger("staff_id")));
			obj.put("kai", o.get("bus_kai"));
			obj.put("jie", o.get("bus_jie"));
			obj.put("shen", o.getInteger("bus_shen"));
			obj.put("shijian", o.getInteger("bus_time")+"/天");
			if(o.getInteger("bus_shen")==0) {
				obj.put("zhuang", "待审核。。");
			}else if(o.getInteger("bus_shen")==1) {
				obj.put("zhuang", "审核已通过");
			}else {
				obj.put("zhuang", "驳回");
				obj.put("bouwhy", o.get("bus_bou"));
			}
			arr.add(obj);
		}
		return arr;
	}
	public JSONArray selectbuka(JSONArray buka,JSONArray arr) {
		for (Object object : buka) {
			JSONObject obj=new JSONObject();
			JSONObject o=(JSONObject)object;
			obj.put("id", o.get("a_id"));
			obj.put("lei", 2);
			obj.put("type","补卡申请");
			obj.put("sdate", o.get("a_shendate"));
			obj.put("name", o.get("Staff_name"));
			obj.put("zhiwei", am_ser.select(o.getInteger("staff_id")));
			obj.put("yuan",o.get("a_why"));
			obj.put("kai", o.get("a_dao"));
			obj.put("staff_id", o.getInteger("staff_id"));
			obj.put("jie", o.get("a_tui"));
			obj.put("bukadate",new SimpleDateFormat("yyyy-MM-dd").format(o.getDate("a_date")));
			obj.put("shen", o.getInteger("a_shen"));
			obj.put("pdate", o.get("a_sdate"));
			if(o.getInteger("a_shen")==0) {
				obj.put("zhuang", "待审核。。");
			}else if(o.getInteger("a_shen")==1) {
				obj.put("zhuang", "审核已通过");
			}else {
				obj.put("zhuang", "驳回");
				obj.put("bouwhy", o.get("a_buo"));
			}
			arr.add(obj);
		}
		return arr;
	}
	public JSONArray selectjiaban(JSONArray shengqing,JSONArray arr) {
		for (Object object : shengqing) {
			JSONObject obj=new JSONObject();
			JSONObject o=(JSONObject)object;
			obj.put("lei", 3);
			obj.put("id", o.get("O_ID"));
			obj.put("type","加班申请");
			obj.put("sdate", o.get("O_DATE"));
			obj.put("staff_id", o.getInteger("staff_id"));
			obj.put("name", o.get("Staff_name"));
			obj.put("pdate", o.get("O_SDATE"));
			obj.put("zhiwei", am_ser.select(o.getInteger("staff_id")));
			obj.put("yuan",o.get("O_WHY"));
			if(o.getInteger("OX_TYPE")==0) {
				obj.put("leixing", "工作日加班");
				obj.put("shijian", o.getInteger("O_TIME")+"/h");
			}else if(o.getInteger("OX_TYPE")==1) {
				obj.put("leixing", "休息日加班");
				obj.put("shijian", o.getInteger("O_TIME")+"/天");
			}else {
				obj.put("leixing", "节假日加班");
				obj.put("shijian", o.getInteger("O_TIME")+"/天");
			}
			obj.put("kai", o.get("O_KAI"));
			obj.put("jie", o.get("O_JIE"));
			obj.put("shen", o.getInteger("O_SHEN"));
			if(o.getInteger("O_SHEN")==0) {
				obj.put("zhuang", "待审核。。");
			}else if(o.getInteger("O_SHEN")==1) {
				obj.put("zhuang", "审核已通过");
			}else {
				obj.put("zhuang", "驳回");
				obj.put("bouwhy", o.get("O_BOU"));
			}
			arr.add(obj);
		}
		return arr;
	}
	public JSONArray selectqingjia(JSONArray qingjia,JSONArray arr) {
		for (Object object : qingjia) {
			JSONObject obj=new JSONObject();
			JSONObject o=(JSONObject)object;
			obj.put("lei", 4);
			obj.put("id", o.get("L_ID"));
			if(o.getInteger("L_CHU")==1) {
				obj.put("type","请假申请");
				
			}else {
				obj.put("type","请假补录");
				
			}
			obj.put("staff_id", o.getInteger("staff_id"));
			obj.put("pdate", o.get("L_SDATE"));
			obj.put("yuan",o.get("L_WHY"));
			obj.put("noyuan",o.get("L_NOWHY"));
			obj.put("sdate", o.get("L_DATE"));
			obj.put("name", o.get("Staff_name"));
			obj.put("zhiwei", am_ser.select(o.getInteger("staff_id")));
			if(o.getInteger("L_CLASS")==1) {
				obj.put("leixing", "事假");
			}else if(o.getInteger("L_CLASS")==2) {
				obj.put("leixing", "病假");
			}else {
				obj.put("leixing", "其他");
			}
			if(o.getInteger("L_TYPE")==1) {
				obj.put("duan", "全天");
			}else if(o.getInteger("L_TYPE")==2) {
				obj.put("duan", "上午");
			}else {
				obj.put("duan", "下午");
			}
			obj.put("shijian", o.getBigDecimal("L_TIME")+"/天");
			obj.put("kai", o.get("L_KAI"));
			obj.put("jie", o.get("L_JIE"));
			obj.put("shen", o.getInteger("L_SHEN"));
			if(o.getInteger("L_SHEN")==0) {
				obj.put("zhuang", "待审核。。");
			}else if(o.getInteger("L_SHEN")==1) {
				obj.put("zhuang", "审核已通过");
			}else {
				obj.put("zhuang", "驳回");
				obj.put("bouwhy", o.get("L_BOU"));
			}
			arr.add(obj);
		}
		return arr;
	}
	public JSONArray selectteach(JSONArray teach,JSONArray arr) {
		for (Object object : teach) {
			JSONObject obj=new JSONObject();
			JSONObject o=(JSONObject)object;
			if(o.get("apply_type").equals("申请讲师")) {
				obj.put("id", o.get("apply_id"));
				obj.put("lei", 5);			
				obj.put("type",o.get("apply_type"));				
				obj.put("staff_id", o.getInteger("staff_id"));
				obj.put("pdate", o.get("pxapproval_time"));
				obj.put("yuan",o.get("apply_reason"));
				obj.put("sdate", o.get("apply_time"));
				obj.put("name", o.get("Staff_name"));
				obj.put("zhiwei", am_ser.select(o.getInteger("staff_id")));
				obj.put("shen", o.getInteger("pxapproval_state"));
				
				if(o.getInteger("pxapproval_state")==1) {
					obj.put("zhuang", "待审核。。");
				}else if(o.getInteger("pxapproval_state")==2) {
					obj.put("zhuang", "驳回");
					obj.put("bouwhy", o.get("pxapproval_bou"));
				}else {				
					obj.put("zhuang", "审核已通过");
				}
				arr.add(obj);
			}
		}
		return arr;
	}
	public JSONArray selectteach1(JSONArray teach,JSONArray arr) {
		for (Object object : teach) {
			JSONObject obj=new JSONObject();
			JSONObject o=(JSONObject)object;
			if(o.get("apply_type").equals("申请培训")) {
				obj.put("id", o.get("apply_id"));
				obj.put("lei", 7);			
				obj.put("type",o.get("apply_type"));				
				obj.put("staff_id", o.getInteger("staff_id"));
				obj.put("pdate", o.get("pxapproval_time"));
				obj.put("yuan",o.get("apply_reason"));
				obj.put("sdate", o.get("apply_time"));
				obj.put("name", o.get("Staff_name"));
				obj.put("zhiwei", am_ser.select(o.getInteger("staff_id")));
				obj.put("shen", o.getInteger("pxapproval_state"));
				if(o.getInteger("pxapproval_state")==1) {
					obj.put("zhuang", "待审核。。");
				}else if(o.getInteger("pxapproval_state")==2) {
					obj.put("zhuang", "驳回");
					obj.put("bouwhy", o.get("pxapproval_bou"));
				}else {				
					obj.put("zhuang", "审核已通过");
				}
				arr.add(obj);
			}
			
		}
		return arr;
	}
	public void xiugaiteach(int id,int shen,String str) {		
		JSONArray apply = j_map.findapplyid(id);
		int px = 0;
		int staff_ids=0;
		for (Object object : apply) {
			JSONObject objs=(JSONObject) object;
			px=(int) objs.get("pxapproval_id");
			staff_ids=(int) objs.get("staff_id");
		}		
		Approval app = ad.findById(px).get();
		if(shen==2) {
			app.setPxapprovalBou(str);
			app.setPxapprovalState("2");
		}else {
			app.setPxapprovalState("3");
			Teacher teachs=new Teacher();
			Staff staffs=sd.findById(staff_ids).get();
			teachs.setStaff(staffs);	
			td.save(teachs);
		}
		app.setPxapprovalTime(new Date());
		app.setPxapprovalIf("1");
		ad.save(app);
		
					
	}
	public void xiugaiteach1(int id,int shen,String str) {		
		JSONArray applys = j_map.findapplyid(id);
		int px = 0;
		int staff_ids=0;
		for (Object object : applys) {
			JSONObject objs=(JSONObject) object;
			px=(int) objs.get("pxapproval_id");
			staff_ids=(int) objs.get("staff_id");
		}		
		Approval app = ad.findById(px).get();
		if(shen==2) {
			app.setPxapprovalBou(str);
			app.setPxapprovalState("2");
		}else {
			app.setPxapprovalState("3");
		}
		app.setPxapprovalTime(new Date());
		app.setPxapprovalIf("1");
		ad.save(app);			
	}
	public JSONArray findsubsh(JSONArray teach,JSONArray arr) {
		for (Object object : teach) {
			JSONObject obj=new JSONObject();
			JSONObject o=(JSONObject)object;
			obj.put("lei", 6);
			obj.put("id", o.get("sub_id"));
			obj.put("type",o.get("sub_type"));
			obj.put("sdate", o.get("sub_date"));
			obj.put("staff_id", o.getInteger("staff_id"));
			obj.put("name", o.get("Staff_name"));
			obj.put("pdate", o.get("sub_spdate"));
			obj.put("zhiwei", am_ser.select(o.getInteger("staff_id")));
			obj.put("yuan",o.get("sub_reason"));
			obj.put("shen", o.getInteger("sub_static"));
			if(o.getInteger("sub_static")==0) {
				obj.put("zhuang", "待审核。。");
			}else if(o.getInteger("sub_static")==1) {
				obj.put("zhuang", "审核已通过");
			}else if(o.getInteger("sub_static")==3) {
				obj.put("zhuang", "审核已通过");
			}else {
				obj.put("zhuang", "驳回");
				obj.put("bouwhy", o.get("sub_fanbo"));
			}
			arr.add(obj);
		}
		return arr;
	}
	public void xgsub(int id,int shen,String str) {
		SalarySub sub=ssd.findById(id).get();
		sub.setSubSpdate(new Date());
		if(shen==2) {
			sub.setSubStatic(2);
			sub.setSubFanbo(str);
		}else {
			sub.setSubStatic(1);
		}
		ssd.save(sub);
	}
	//招聘申请
		public JSONArray rapplyfsp(JSONArray qingjia,JSONArray arr) {
			for (Object object : qingjia) {
				JSONObject obj=new JSONObject();
				JSONObject o=(JSONObject)object;
				obj.put("lei", 8);
				obj.put("id", o.get("rapplyid"));
				obj.put("staff_id", o.getInteger("staff_id"));
				obj.put("pdate", o.get("rapplysptime"));
				obj.put("yuan",o.get("rapplyreason"));
				obj.put("sdate", o.get("rapplytime"));
				obj.put("name", o.get("Staff_name"));
				obj.put("zhiwei", am_ser.select(o.getInteger("staff_id")));
				obj.put("kai", o.get("rapplystart"));
				obj.put("jie", o.get("rapplyend"));
				obj.put("shen", o.getInteger("rapplyspstatue"));
				if(o.getInteger("rapplyspstatue")==0) {
					obj.put("zhuang", "待审核。。");
				}else if(o.getInteger("rapplyspstatue")==1) {
					obj.put("zhuang", "审核已通过");
				}else {
					obj.put("zhuang", "驳回");
					obj.put("bouwhy", o.get("rapplyspstatue"));
				}
				arr.add(obj);
			}
			return arr;
		}
	public void rapply(int id,int shen,String str) {
			Rapply rapp = ry.findById(id).get();
			rapp.setRapplysptime(new Date());
			if(shen==2) {
				rapp.setRapplybou(str);
				rapp.setRapplyspstatue(2);
			}else {
				rapp.setRapplyspstatue(1);
			}		
			ry.save(rapp);			
		}
	
}
